var indexVm = new Vue({
	el:'#mui-login-content',
	data:function() {
		return {
			userNumber: '',
			password:'',
			accountPlease: '',
			passwordPlease: '',
			userNumberError: '',
			passwordError: ''
		}
	},
	mounted: function() {
		var self = this;
		mui.init();
		mui.plusReady(function () {
		    //设置状态栏的颜色
		    // plus.navigator.setStatusBarStyle("UIStatusBarStyleBlackOpaque");
		    // plus.navigator.setStatusBarBackground('#00B0F0');
			
			self.initValidator();
			self.initEvent();
		})
	},
	methods:{
		initValidator:function(){
			this.constraints = {
				username: {
				    presence: true,
					length: {
					  maximum: 10,
					  message: "账号最多10位!",
					  }
				  },
				password: {
					presence: true,
					length: {
					  maximum: 20,
					  message: "密码最多20位!"
					}
				}
			}
			// 为空时的信息
			validate.validators.presence.message = "不能为空!";
		},
		initEvent: function() {
			/**
			 * 防止键盘弹起界面布局崩坏
			 */
			//获取原始窗口的高度
			var originalHeight = document.documentElement.clientHeight || document.body.clientHeight;
			var topGraphDom = document.getElementById("login-top-wrapper");
			// plus.webview.currentWebview().setStyle({
			// 	height: originalHeight
			// });
			window.onresize = function() {
				//软键盘弹起与隐藏  都会引起窗口的高度发生变化
				var resizeHeight = document.documentElement.clientHeight || document.body.clientHeight;
				if(resizeHeight * 1 < originalHeight * 1) { //resizeHeight<originalHeight证明窗口被挤压了
					// plus.navigator.setStatusBarStyle("dark");
					// plus.navigator.setStatusBarBackground('#ffffff');
					topGraphDom.classList.add('animated', 'fadeOutUpBig');
				} else {
					// plus.navigator.setStatusBarStyle("UIStatusBarStyleBlackOpaque");
					// plus.navigator.setStatusBarBackground('#00B0F0');
					topGraphDom.classList.remove('animated', 'fadeOutUpBig');
				}
			};
		},
		login: function() {
			var data = {};
			if(this.userNumber) {
				data['username'] = this.userNumber;
			}
			if(this.password) {
				data['password'] = this.password;
			}
			var errors = validate(data, this.constraints);
			
			if(errors) {
				this.userNumberError = errors.username;
				this.passwordError = errors.password;
			} else {
				this.userNumberError = '';
				this.passwordError = '';
				this.submitLogin();
			}
		},
		submitLogin: function() {
			var self = this;
			var url = config.requestBaseUrl + 'login';
			mui.ajax(url,{
				data:{
					account: this.userNumber,
					pass: this.password
				},
				dataType:'json',//服务器返回json格式数据
				type:'post',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					if(data.isSuccess) {
						var thisData = data.data;
						plus.storage.setItem('islogined', 'true');
						plus.storage.setItem('account', self.account);
						plus.storage.setItem('name', thisData.name);
						plus.storage.setItem('userid', String(thisData.id));
						plus.storage.setItem('type', thisData.type);
						plus.storage.setItem('role', thisData.role);
						plus.storage.setItem('depart', thisData.depart);
						plus.storage.setItem('departid', String(thisData.departid));
						
						var launchPage = plus.webview.getLaunchWebview();
						mui.fire(launchPage,'logined',{});
						
						var thisPage = plus.webview.currentWebview();
						thisPage.close();
						
						mui.toast("登录成功!");
					} else {
						mui.toast(data.msg);
					}
				},
				error:function(xhr,type,errorThrown){
					console.log(errorThrown);
					mui.toast("登录失败!");
				}
			});
		}
	}
})